home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / Crypto / PublicKey / pubkey.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.4 KB  |  204 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __revision__ = '$Id: pubkey.py,v 1.11 2003/04/03 20:36:14 akuchling Exp $'
  5. import types
  6. import warnings
  7. from Crypto.Util.number import *
  8.  
  9. class pubkey:
  10.     
  11.     def __init__(self):
  12.         pass
  13.  
  14.     
  15.     def __getstate__(self):
  16.         '''To keep key objects platform-independent, the key data is
  17.         converted to standard Python long integers before being
  18.         written out.  It will then be reconverted as necessary on
  19.         restoration.'''
  20.         d = self.__dict__
  21.         for key in self.keydata:
  22.             if d.has_key(key):
  23.                 d[key] = long(d[key])
  24.                 continue
  25.         
  26.         return d
  27.  
  28.     
  29.     def __setstate__(self, d):
  30.         '''On unpickling a key object, the key data is converted to the big
  31. number representation being used, whether that is Python long
  32. integers, MPZ objects, or whatever.'''
  33.         for key in self.keydata:
  34.             if d.has_key(key):
  35.                 self.__dict__[key] = bignum(d[key])
  36.                 continue
  37.         
  38.  
  39.     
  40.     def encrypt(self, plaintext, K):
  41.         '''encrypt(plaintext:string|long, K:string|long) : tuple
  42.         Encrypt the string or integer plaintext.  K is a random
  43.         parameter required by some algorithms.
  44.         '''
  45.         wasString = 0
  46.         if isinstance(plaintext, types.StringType):
  47.             plaintext = bytes_to_long(plaintext)
  48.             wasString = 1
  49.         
  50.         if isinstance(K, types.StringType):
  51.             K = bytes_to_long(K)
  52.         
  53.         ciphertext = self._encrypt(plaintext, K)
  54.         if wasString:
  55.             return tuple(map(long_to_bytes, ciphertext))
  56.         return ciphertext
  57.  
  58.     
  59.     def decrypt(self, ciphertext):
  60.         """decrypt(ciphertext:tuple|string|long): string
  61.         Decrypt 'ciphertext' using this key.
  62.         """
  63.         wasString = 0
  64.         if not isinstance(ciphertext, types.TupleType):
  65.             ciphertext = (ciphertext,)
  66.         
  67.         if isinstance(ciphertext[0], types.StringType):
  68.             ciphertext = tuple(map(bytes_to_long, ciphertext))
  69.             wasString = 1
  70.         
  71.         plaintext = self._decrypt(ciphertext)
  72.         if wasString:
  73.             return long_to_bytes(plaintext)
  74.         return plaintext
  75.  
  76.     
  77.     def sign(self, M, K):
  78.         '''sign(M : string|long, K:string|long) : tuple
  79.         Return a tuple containing the signature for the message M.
  80.         K is a random parameter required by some algorithms.
  81.         '''
  82.         if not self.has_private():
  83.             raise error, 'Private key not available in this object'
  84.         self.has_private()
  85.         if isinstance(M, types.StringType):
  86.             M = bytes_to_long(M)
  87.         
  88.         if isinstance(K, types.StringType):
  89.             K = bytes_to_long(K)
  90.         
  91.         return self._sign(M, K)
  92.  
  93.     
  94.     def verify(self, M, signature):
  95.         '''verify(M:string|long, signature:tuple) : bool
  96.         Verify that the signature is valid for the message M;
  97.         returns true if the signature checks out.
  98.         '''
  99.         if isinstance(M, types.StringType):
  100.             M = bytes_to_long(M)
  101.         
  102.         return self._verify(M, signature)
  103.  
  104.     
  105.     def validate(self, M, signature):
  106.         warnings.warn('validate() method name is obsolete; use verify()', DeprecationWarning)
  107.  
  108.     
  109.     def blind(self, M, B):
  110.         '''blind(M : string|long, B : string|long) : string|long
  111.         Blind message M using blinding factor B.
  112.         '''
  113.         wasString = 0
  114.         if isinstance(M, types.StringType):
  115.             M = bytes_to_long(M)
  116.             wasString = 1
  117.         
  118.         if isinstance(B, types.StringType):
  119.             B = bytes_to_long(B)
  120.         
  121.         blindedmessage = self._blind(M, B)
  122.         if wasString:
  123.             return long_to_bytes(blindedmessage)
  124.         return blindedmessage
  125.  
  126.     
  127.     def unblind(self, M, B):
  128.         '''unblind(M : string|long, B : string|long) : string|long
  129.         Unblind message M using blinding factor B.
  130.         '''
  131.         wasString = 0
  132.         if isinstance(M, types.StringType):
  133.             M = bytes_to_long(M)
  134.             wasString = 1
  135.         
  136.         if isinstance(B, types.StringType):
  137.             B = bytes_to_long(B)
  138.         
  139.         unblindedmessage = self._unblind(M, B)
  140.         if wasString:
  141.             return long_to_bytes(unblindedmessage)
  142.         return unblindedmessage
  143.  
  144.     
  145.     def can_sign(self):
  146.         '''can_sign() : bool
  147.         Return a Boolean value recording whether this algorithm can
  148.         generate signatures.  (This does not imply that this
  149.         particular key object has the private information required to
  150.         to generate a signature.)
  151.         '''
  152.         return 1
  153.  
  154.     
  155.     def can_encrypt(self):
  156.         '''can_encrypt() : bool
  157.         Return a Boolean value recording whether this algorithm can
  158.         encrypt data.  (This does not imply that this
  159.         particular key object has the private information required to
  160.         to decrypt a message.)
  161.         '''
  162.         return 1
  163.  
  164.     
  165.     def can_blind(self):
  166.         '''can_blind() : bool
  167.         Return a Boolean value recording whether this algorithm can
  168.         blind data.  (This does not imply that this
  169.         particular key object has the private information required to
  170.         to blind a message.)
  171.         '''
  172.         return 0
  173.  
  174.     
  175.     def size(self):
  176.         '''size() : int
  177.         Return the maximum number of bits that can be handled by this key.
  178.         '''
  179.         return 0
  180.  
  181.     
  182.     def has_private(self):
  183.         '''has_private() : bool
  184.         Return a Boolean denoting whether the object contains
  185.         private components.
  186.         '''
  187.         return 0
  188.  
  189.     
  190.     def publickey(self):
  191.         '''publickey(): object
  192.         Return a new key object containing only the public information.
  193.         '''
  194.         return self
  195.  
  196.     
  197.     def __eq__(self, other):
  198.         '''__eq__(other): 0, 1
  199.         Compare us to other for equality.
  200.         '''
  201.         return self.__getstate__() == other.__getstate__()
  202.  
  203.  
  204.